<?
require_once('functions.php');

function GetOrders()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM 2012WP_Orders');
}
function BlankOrder()
{
        return array('Transaction'=>null,'Cost'=>null, 'ShippingCost'=>null, 'NumItems'=>null, 'Users_id'=>null, 'OrderStatus_id'=>null, 'ShippingMethods_id'=>null, 'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function GetOrder($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM 2012WP_Orders WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}
function SaveOrder(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql =  "Insert 2012WP_Orders (Transaction, Cost, ShippingCost, NumItems, 2012WP_Users_id, 2012WP_OrderStatus_id, 2012WP_ShippingMethods_id, created_at) "
                        .       " Values ('$row2[Transaction]','$row2[Cost]', '$row2[ShippingCost]', '$row2[NumItems]', '$row2[Users_id]',  '$row2[OrderStatus_id]', '$row2[ShippingMethods_id]', NOW() ) ";
        }else{
                $sql =  "UPDATE 2012WP_Users "
                        .       "Set Transaction='$row2[Transaction]', Cost='$row2[Cost]', ShippingCost='$row2[ShippingCost]', NumItems='$row2[NumItems]', 2012WP_Users_id='$row2[Users_id]', 2012WP_OrderStatus_id='$row2[OrderStatus_id]', 2012WP_ShippingMethods_id='$row2[ShippingMethods_id]' "
                        .       "WHERE id=$row2[id] ";
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function DeleteOrder($id)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM 2012WP_Orders WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateOrder($row)
{
        $errors = array();
		if(!is_numeric($row['OrderStatus_id'])) $errors['OrderStatus_id'] = 'OrderStatus_id must be a number';
        if(empty($row['OrderStatus_id'])) $errors['OrderStatus_id'] = 'Orderstatus_id is required';
		if(!is_numeric($row['Users_id'])) $errors['Users_id'] = 'Users_id must be a number';
        if(empty($row['Users_id'])) $errors['Users_id'] = 'Users_id is required';
		if(!is_numeric($row['NumItems'])) $errors['NumItems'] = 'NumItems must be a number';
        if(empty($row['NumItems'])) $errors['NumItems'] = 'Cost is required';
		if(!is_numeric($row['ShippingCost'])) $errors['ShippingCost'] = 'ShippingCost must be a number';
        if(empty($row['ShippingCost'])) $errors['ShippingCost'] = 'ShippingCost is required';
		if(!is_numeric($row['Cost'])) $errors['Cost'] = 'Cost must be a number';
        if(empty($row['Cost'])) $errors['Cost'] = 'Cost is required';
		if(!is_numeric($row['Transaction'])) $errors['Transaction'] = 'Transaction must be a number';
        if(empty($row['Transaction'])) $errors['Transaction'] = 'Transaction is required';
		if(!is_numeric($row['ShippingMethods_id'])) $errors['ShippingMethods_id'] = 'ShippingMethods_id must be a number';
        if(empty($row['ShippingMethods_id'])) $errors['ShippingMethods_id'] = 'ShippingMethods_id is required';
       
        return count($errors) == 0 ? true : $errors;
}
